package com.itheima.controller;

import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.common.dtos.ResponseResult;
import com.itheima.service.ArticleService;
import freemarker.template.TemplateException;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * 文章微服务的相关接口
 *
 * @author yunfei
 * @date 2022年10月14日 17:02
 */
@RestController
@RequestMapping("api/v1/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * APP端文章首页数据请求
     *
     * @return
     */
    @PostMapping("load")
    public ResponseResult load(@RequestBody ArticleHomeDto homeDto) {
        return articleService.load(homeDto);
    }

    /**
     * 下拉加载最新数据
     *
     * @return
     */
    @PostMapping("loadnew")
    public ResponseResult loadnew(@RequestBody ArticleHomeDto homeDto) {
        return articleService.loadnew(homeDto);
    }

    /**
     * 上拉加载更多数据
     *
     * @return
     */
    @PostMapping("loadmore")
    public ResponseResult loadmore(@RequestBody ArticleHomeDto homeDto) {
        return articleService.loadmore(homeDto);
    }

    /**
     * 发布文章
     */
    @PostMapping("save")
    public ResponseResult saveArticle(@RequestBody ArticleDto articleDto) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, InternalException, TemplateException, XmlParserException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException {
        return articleService.saveArticle(articleDto);
    }

    // 给ES做测试用
    // 根据一个文章id 查询文章的详情
    @GetMapping("findOneById/{id}")
    public ResponseResult findOneById(@PathVariable("id") long articleId) {
        return articleService.findOneById(articleId);
    }

    @GetMapping("findAll")
    public ResponseResult findAll() {
        return articleService.findAll();
    }

    /**
     * 文章行为回显
     *
     * @param dto
     * @return
     */
    @PostMapping("/load_article_behavior")
    public ResponseResult loadArticleBehavior(@RequestBody ArticleInfoDto dto) {
        return articleService.loadArticleBehavior(dto);
    }


    /**
     * 查询最近5天的所有文章数据
     *
     * @return
     */
    @GetMapping("selectFiveDaysArticle")
    public ResponseResult selectFiveDaysArticle() {
        return articleService.selectFiveDaysArticle();
    }

}
